package com.ruoyi.business.controller;

import com.ruoyi.business.domain.HistoricFollow;
import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 潜在客户Controller
 *
 * @author wolfcode
 * @date 2021-07-26
 */
@Controller
@RequestMapping("/business/potentialCustomer")
public class PotentialCustomerController extends BaseController {
    private String prefix = "business/potentialCustomer";

    @Autowired
    private IPotentialCustomerService potentialCustomerService;


    @GetMapping()
    public String potentialCustomer() {
        return prefix + "/potentialCustomer";
    }

    /**
     * 查询潜在客户列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PotentialCustomer potentialCustomer) {
        startPage();
        if (SecurityUtils.getSubject().hasRole("salesman")) {
            Long userId = ShiroUtils.getUserId();
            List<PotentialCustomer> list = potentialCustomerService.selectPotentialCustomerListByUserId(userId);
            return getDataTable(list);
        }
        List<PotentialCustomer> list = potentialCustomerService.selectPotentialCustomerList(potentialCustomer);
        return getDataTable(list);
    }

    /**
     * 导出潜在客户列表
     */
    @Log(title = "潜在客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PotentialCustomer potentialCustomer) {
        List<PotentialCustomer> list = potentialCustomerService.selectPotentialCustomerList(potentialCustomer);
        ExcelUtil<PotentialCustomer> util = new ExcelUtil<PotentialCustomer>(PotentialCustomer.class);
        return util.exportExcel(list, "潜在客户数据");
    }

    /**
     * 新增潜在客户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PotentialCustomer potentialCustomer) {
        return toAjax(potentialCustomerService.insertPotentialCustomer(potentialCustomer));
    }

    /**
     * 修改潜在客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PotentialCustomer potentialCustomer = potentialCustomerService.selectPotentialCustomerById(id);
        mmap.put("potentialCustomer", potentialCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PotentialCustomer potentialCustomer) {
        return toAjax(potentialCustomerService.updatePotentialCustomer(potentialCustomer));
    }

    /**
     * 删除潜在客户
     */
    @Log(title = "潜在客户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(potentialCustomerService.deletePotentialCustomerByIds(ids));
    }


    /**
     * 新增跟进
     */
    @GetMapping("/follow/{id}")
    public String follow(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/follow";
    }

    /**
     * 新增保存跟进
     */
    @PostMapping("/follow")
    @ResponseBody
    public AjaxResult followSave(HistoricFollow historicFollow) {
        return toAjax(potentialCustomerService.insertHistoricFollow(historicFollow));
    }


    /**
     * 加载审批历史弹窗
     */
    @GetMapping("/historyList/{id}")
    public String historyList(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/historyList";
    }

    @PostMapping("/historyList")
    @ResponseBody
    public TableDataInfo listHistory(Long id) {
        List<HistoricFollow> list = potentialCustomerService.selectHistoryList(id);
        return getDataTable(list);
    }
}
